﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace homework13
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            LoadDirectories(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void LoadDirectories(string path)
        {
            try
            {
                // 清空树形视图和列表
                treeView1.Nodes.Clear();
                listView1.Items.Clear();

                // 添加根目录
                TreeNode rootNode = new TreeNode(Path.GetFileName(path));
                rootNode.Tag = path;
                treeView1.Nodes.Add(rootNode);

                // 加载子目录
                string[] directories = Directory.GetDirectories(path);
                foreach (string directory in directories)
                {
                    TreeNode node = new TreeNode(Path.GetFileName(directory));
                    node.Tag = directory;
                    rootNode.Nodes.Add(node);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
        }
     
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            string selectedPath = e.Node.Tag as string;
            if (selectedPath != null)
            {
                LoadFiles(selectedPath);
            }
        }
        private void LoadFiles(string path)
        {
            try
            {
                // 清空列表
                listView1.Items.Clear();

                // 添加文件夹
                string[] directories = Directory.GetDirectories(path);
                foreach (string directory in directories)
                {
                    ListViewItem item = new ListViewItem(Path.GetFileName(directory));
                    item.ImageIndex = 0;
                    listView1.Items.Add(item);
                }

                // 添加文件
                string[] files = Directory.GetFiles(path);
                foreach (string file in files)
                {
                    ListViewItem item = new ListViewItem(Path.GetFileName(file));
                    item.ImageIndex = 1;
                    item.Tag = file;
                    listView1.Items.Add(item);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
        }
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                string selectedFilePath = listView1.SelectedItems[0].Tag as string;
                if (selectedFilePath != null && Path.GetExtension(selectedFilePath).Equals(".txt", StringComparison.OrdinalIgnoreCase))
                {
                    // 如果是txt文件，打开
                    System.Diagnostics.Process.Start(selectedFilePath);
                }
            }
        }

        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void toolStripTextBox1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Menu Opened");
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            MessageBox.Show("Toolbar button clicked");
        }

        private void toolStripLabel1_Click(object sender, EventArgs e)
        {

        }
    }
}
